package com.gxar.quick.ar.server.base.model.protocol;

import com.gxar.common.utils.StringSpliceUtils;
import com.gxar.common.utils.StringUtils;
import com.gxar.quick.ar.server.base.common.utils.NumberUtils;
import lombok.Data;

import java.util.Objects;

/**
 * AR物体四元数信息
 *
 * @author zxh
 * @version 1.0
 * @created 2024/01/02.
 */
@Data
public class ArQuaternion {

    private Double x;

    private Double y;

    private Double z;

    private Double w;

    public ArQuaternion() {

    }

    public ArQuaternion(Double x, Double y, Double z, Double w) {
        this.x = x;
        this.y = y;
        this.z = z;
        this.w = w;
    }

    public ArQuaternion(ArQuaternion input) {
        this.x = input.x;
        this.y = input.y;
        this.z = input.z;
        this.w = input.w;
    }

    @Override
    public String toString() {
        return StringSpliceUtils.splice("{} {} {} {}",
                NumberUtils.formatDouble(x),
                NumberUtils.formatDouble(y),
                NumberUtils.formatDouble(z),
                NumberUtils.formatDouble(w)
        );
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ArQuaternion that = (ArQuaternion) o;
        return Objects.equals(x, that.x) && Objects.equals(y, that.y) && Objects.equals(z, that.z) && Objects.equals(w, that.w);
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y, z, w);
    }

    public static ArQuaternion create(Double x, Double y, Double z, Double w) {
        return new ArQuaternion(x, y, z, w);
    }

    public static ArQuaternion from(String inputStr) {
        if (StringUtils.isEmpty(inputStr)) {
            return null;
        }

        String[] inputArrays = inputStr.split(" ");
        if (inputArrays.length != 4) {
            return null;
        }

        Double x = Double.parseDouble(inputArrays[0]);
        Double y = Double.parseDouble(inputArrays[1]);
        Double z = Double.parseDouble(inputArrays[2]);
        Double w = Double.parseDouble(inputArrays[3]);

        return new ArQuaternion(x, y, z, w);
    }
}
